package com.guigu.qxl.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.pojo.DModuleDetails;
import com.guigu.pojo.DesignModuleDetailsVo;
import com.guigu.qxl.pojo.QDesignModule;
import com.guigu.qxl.pojo.PageVo;
import com.guigu.qxl.service.DesignModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("DesignModule")
public class DesignModuleController {
    @Autowired
    DesignModuleService designModuleService;

    @RequestMapping("designModuleRegister")
    public Map designModuleRegister(@RequestBody DesignModuleDetailsVo designModuleDetailsVo){
        Map map=new HashMap();
        map.put("code","-1");
        map.put("msg","登记失败");
        String [] arg1=designModuleDetailsVo.getSelect1().split("/");
        designModuleDetailsVo.setFirstKindId(arg1[0]);
        designModuleDetailsVo.setFirstKindName(arg1[1]);
        String [] arg2=designModuleDetailsVo.getSelect2().split("/");
        designModuleDetailsVo.setSecondKindId(arg2[0]);
        designModuleDetailsVo.setSecondKindName(arg2[1]);
        String [] arg3=designModuleDetailsVo.getSelect3().split("/");
        designModuleDetailsVo.setThirdKindId(arg3[0]);
        designModuleDetailsVo.setThirdKindName(arg3[1]);
        int num=0;
        for (DModuleDetails d: designModuleDetailsVo.getList()) {
            num=num+d.getSubtotal();
        }
        designModuleDetailsVo.setCostPriceSum(num);
        if(designModuleService.designModuleRegister(designModuleDetailsVo)){
            map.put("code","1");
            map.put("msg","登记成功");
        }
        return map;
    }
    @RequestMapping("designModuleChange")
    public Map designModuleChange(@RequestBody DesignModuleDetailsVo designModuleDetailsVo){
        Map map=new HashMap();
        map.put("code","-1");
        map.put("msg","修改失败");
        if(designModuleService.designModuleChange(designModuleDetailsVo)){
            map.put("code","1");
            map.put("msg","修改成功");
        }
        return map;
    }
    @RequestMapping("showDesignModuleById")
    public QDesignModule showDesignModuleById(QDesignModule designModule){
        return designModuleService.showDesignModuleById(designModule.getId());
    }
    @RequestMapping("showDesignModuleDetailsByParentId")
    public List<DModuleDetails> showDesignModuleDetailsByParentId(QDesignModule designModule){
        return designModuleService.showDesignModuleDetailsByParentId(designModule.getId());
    }
    @GetMapping("pageDesignModule")
    public PageVo<QDesignModule> pageDesignModule(Integer pageNum, Integer pageSize){
        PageInfo<QDesignModule> pageInfo=designModuleService.pageDesignModule(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @GetMapping("pageS0010DesignModule")
    public PageVo<QDesignModule> pageS0010DesignModule(Integer pageNum, Integer pageSize){
        PageInfo<QDesignModule> pageInfo=designModuleService.pageS0010DesignModule(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @GetMapping("pageS0011DesignModule")
    public PageVo<QDesignModule> pageS0011DesignModule(Integer pageNum, Integer pageSize){
        PageInfo<QDesignModule> pageInfo=designModuleService.pageS0011DesignModule(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @RequestMapping("designDesignModuleVheckOk")
    public Map designDesignModuleVheckOk(@RequestBody QDesignModule designModule){
        System.out.println(designModule);
        Map map=new HashMap();
        map.put("code","-1");
        map.put("msg","上传失败");
        if(designModuleService.designDesignModuleVheckOk(designModule)){
            map.put("code","1");
            map.put("msg","上传成功");
        }
        return map;
    }
}
